#include <iostream>
#include "process_frame.h"
#include "onnx_caller.h"

FrameProcessor* FrameProcessor::m_instance = nullptr;

FrameProcessor::FrameProcessor()
{
    m_idx = 0;
    result = new double[4];
}

FrameProcessor* FrameProcessor::getInstance()
{
    if(m_instance == nullptr)
        m_instance = new FrameProcessor();

    return m_instance;
}

void FrameProcessor::process_frame(const cv::Mat& frame)
{
    std::cout << "Processing frame..." << std::endl;
}

void FrameProcessor::yuv422_to_mat(AVFrame *frame)
{
    int width = frame->width, height = frame->height;
    cv::Mat tmp_img = cv::Mat::zeros( height*3/2, width, CV_8UC1 );    
    memcpy( tmp_img.data, frame->data[0], width*height );
    memcpy( tmp_img.data + width*height, frame->data[1], width*height/4 );
    memcpy( tmp_img.data + width*height*5/4, frame->data[2], width*height/4 );

    cv::Mat bgr;
    cv::cvtColor( tmp_img, bgr, cv::COLOR_YUV2BGR_I420);

    // OnnxCaller *pOnnxCaller = OnnxCaller::getInstance();
    // pOnnxCaller->ProcessImage(bgr);
    cv::putText(bgr, "REC", cv::Point(30, 70), cv::FONT_HERSHEY_PLAIN, 3.0, cv::Scalar(0, 0, 255), 5);

    cv::cvtColor(bgr, tmp_img, cv::COLOR_BGR2YUV_I420);
    memcpy(frame->data[0], tmp_img.data, width * height);
    memcpy(frame->data[1], tmp_img.data + width*height, width*height/4 );
    memcpy(frame->data[2], tmp_img.data + width*height*5/4, width*height/4 );
}

void FrameProcessor::ProcessYUVFrame(AVFrame* pFrameYUV)
{
    // 将AVFrame转换为OpenCV Mat
    yuv422_to_mat(pFrameYUV);
}

